package com.mallcai.bigdata.ladon.service.report.impl;

import lombok.Getter;
import org.springframework.context.ApplicationEvent;

/**
 * @author feixy
 * @version 1.0
 * @date 2019-09-05 17:42
 */
public class MetaRefreshEvent extends ApplicationEvent {

    @Getter
    private final String type;

    @Getter
    private final String key;

    public MetaRefreshEvent(String type, String key) {
        super(String.format("%s-%s",type, key));
        this.type = type;
        this.key = key;
    }

    public static MetaRefreshEvent newDatasourceChangeEvent(long dsId){
        return new MetaRefreshEvent(MetaRefreshType.DATA_SOURCE.getCode(), String.valueOf(dsId));
    }

    public static MetaRefreshEvent newReportChangeEvent(String code){
        return new MetaRefreshEvent(MetaRefreshType.REPORT.getCode(), code);
    }
    public static MetaRefreshEvent newReportSqlChangeEvent(long sqlId){
        return new MetaRefreshEvent(MetaRefreshType.SQL.getCode(), String.valueOf(sqlId));
    }

    public static MetaRefreshEvent newReportColumnChangeEvent(long reportId){
        return new MetaRefreshEvent(MetaRefreshType.COLUMNS.getCode(), String.valueOf(reportId));
    }

    public static MetaRefreshEvent newReportFieldChangeEvent(long reportId){
        return new MetaRefreshEvent(MetaRefreshType.FIELDS.getCode(), String.valueOf(reportId));
    }

    public String getCacheKey(){
        return String.format("%s-%s", type, key);
    }
}
